package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"hotel/model/mysql"

	"hotel/userrpc/internal/svc"
	"hotel/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserPerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserPerfectLogic {
	return &UserPerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserPerfectLogic) UserPerfect(in *userrpc.UserPerfectRequest) (*userrpc.UserPerfectResponse, error) {
	// todo: add your logic here and delete this line

	var finduser mysql.User
	mysql.DB.Where("user_name = ?", in.Username).Find(&finduser)
	if finduser.ID == 0 {
		return nil, fmt.Errorf("该账号不存在")
	}
	user := mysql.User{
		Model:       gorm.Model{ID: finduser.ID},
		HeaderImage: in.HeaderImage,
		VipName:     in.VipName,
		Sex:         in.Sex,
		Autograph:   in.Autograph,
	}
	mysql.DB.Updates(&user)

	return &userrpc.UserPerfectResponse{
		Id: int64(finduser.ID),
	}, nil
}
